<template>
  <div class="container">
    <a-row>
      <a-col :xs="0" :sm="0" :md="24">
        <div class="classify-container">
          <a-row class="tab-container">
            <a-col
              :md="8"
              :lg="4"
              :class="{ active: activeIndex === item.id }"
              @click="
                () => {
                  activeIndex = item.id
                  search(true)
                }
              "
              v-for="item in classifylist"
              :key="item.id"
            >
              <div class="tab-item">
                {{ item.name }}
              </div>
            </a-col>
          </a-row>
        </div>
      </a-col>
      <a-col :xs="24" :sm="24" :md="0">
        <div class="classify-container">
          <div class="tab-container">
            <div class="tab-content">
              <div
                class="tab-item"
                :class="{ active: activeIndex === item.id }"
                @click="
                  () => {
                    activeIndex = item.id
                    search(true)
                  }
                "
                v-for="item in classifylist"
                :key="item.id"
              >
                {{ item.name }}
              </div>
            </div>
          </div>
          <img
            class="menuTitleIcon"
            src="@/assets/images/icon/browser-more.png"
            @click="showDropdown = true"
          />
          <div class="dropdown-box" v-show="showDropdown">
            <a-flex class="menu-head" justify="space-between" align="center">
              <h4>分类</h4>
              <img
                src="@/assets/images/icon/browser-more.png"
                alt=""
                @click="showDropdown = false"
              />
            </a-flex>
            <div class="menu-content">
              <div
                class="menu-item"
                :class="{ active: activeIndex === item.id }"
                @click="
                  () => {
                    activeIndex = item.id
                    search(true)
                  }
                "
                v-for="item in classifylist"
                :key="item.id"
              >
                {{ item.name }}
              </div>
            </div>
          </div>
        </div>
      </a-col>
    </a-row>
    <a-spin :spinning="loading">
      <a-row class="episode-list" :gutter="16">
        <a-col
          :span="4"
          :xs="8"
          :md="8"
          :lg="4"
          class="book-item"
          v-for="(item, index) in data?.data?.data"
          :key="item.id"
        >
          <NuxtLink :to="`/movie/${item.id}`" class="book-image">
            <img :alt="item.name" loading="lazy" :src="item.cover" />
          </NuxtLink>
          <div class="chapter-count">{{ item.episodeCount }} 集</div>
          <div class="book-name">{{ item.name }}</div>
          <div class="book-name-box">
            <div class="book-name-hover">{{ item.name }}</div>
            <div class="tag-box">
              <NuxtLink
                class="tag-item"
                :to="`/movie/${item.id}`"
                v-for="(category, index) in item?.category"
                :key="category.id"
              >
                {{ category }}
              </NuxtLink>
            </div>
          </div>
        </a-col>
      </a-row>
    </a-spin>
    <renderPagination @handleSizeChange="handleSizeChange" />
  </div>
</template>
<script setup lang="ts">
import { ref } from "vue"
import { request as getCategoryRequest } from "~/api/server/mods/category/getCategory"
import { request as getMoviePageRequest } from "~/api/server/mods/movie/getMoviePage"
import { errorTips } from "@/utils"
import { useRequest } from "~/hooks/UseRequest"

const $route = useRoute()
const showDropdown = ref(false)
const activeIndex = ref(0)
const classifylist = ref<defs.server.CategoryEntity[]>([])
if ($route.query.classifyId) {
  activeIndex.value = Number($route.query.classifyId)
}
getCategory()
async function getCategory() {
  try {
    const { data, success } = await getCategoryRequest({})
    if (success) {
      classifylist.value = data ?? []
    }
  } catch (error: any) {
    errorTips(error.message)
  }
}
const defaultParams = reactive({
  pageSize: 18,
  pageNum: 1,
})
const { run, data, getPagination, renderPagination, loading } = useRequest(
  getMoviePageRequest,
  {
    manual: true,
    pagination: true,
    paginationDefaultConfig: {
      pageSize: 18,
      pageSizeOptions: ["18", "36", "72"],
    },
    defaultParams: [{ ...defaultParams }],
  }
)
function search(is = true) {
  const classify = classifylist.value.find(
    (item) => item.id === activeIndex.value
  )

  run({
    categoryId: classify?.id === 0 ? undefined : classify?.id,
    ...getPagination(is),
  } as any)
}
function handleSizeChange(pageSize: number) {
  defaultParams.pageSize = pageSize
}
</script>
<style lang="scss" scoped>
.classify-container {
  margin: 0.4rem auto 0.4rem;
  width: 100%;
  min-height: 0.4rem;
  background-color: rgba(50, 50, 57, 0.07);
  border-radius: 0.04rem;
  .tab-container {
    align-items: center;
    overflow-x: auto;
    .tab-item {
      text-align: center;
      padding: 0 0.1rem;
      min-width: 1.4rem;
      height: 0.4rem;
      border-radius: 0.04rem;
      font-size: 0.16rem;
      font-weight: 400;
      white-space: break-spaces;
      color: rgba(0, 0, 0, 0.9);
      display: flex;
      align-items: center;
      justify-content: center;
      background-color: hsla(0, 0%, 100%, 0);
      transition: all 0.3s;
      cursor: pointer;
      &:hover {
        color: #ff375f;
      }
    }
    .active {
      color: #ff375f;
      font-weight: 500;
      background-color: rgba(50, 50, 57, 0.07);
    }
  }
}
.episode-list {
  padding-top: 0.88rem;
  .book-item {
    width: 100%;
    height: 4.1rem;
    &:hover {
      .book-image {
        border-radius: 0.08rem 0.08rem 0 0;
      }
      .book-name {
        opacity: 0;
      }
      .book-name-box {
        opacity: 1;
      }
    }
    .book-image {
      width: 100%;
      height: 3.04rem;
      overflow: hidden;
      border-radius: 0.08rem;
      display: flex;
      align-items: center;
      &:hover {
        img {
          transform: scale(1.1);
        }
      }
      img {
        object-fit: cover;
        width: 100%;
        height: 100%;
        transition: all 0.5s;
      }
    }
    .chapter-count {
      display: flex;
      align-items: center;
      border-radius: 0 0 0.08rem 0.08rem;
      background: linear-gradient(180deg, transparent, rgba(0, 0, 0, 0.6));
      position: absolute;
      z-index: 10;
      width: calc(100% - 16px);
      height: 0.56rem;
      top: 2.48rem;
      left: 50%;
      transform: translateX(-50%);
      padding: 0.25rem 0.08rem 0;
      text-align: right;
      font-size: 0.13rem;
      font-weight: 400;
      color: hsla(0, 0%, 100%, 0.8);
      line-height: 0.18rem;
      box-sizing: border-box;
    }
    .book-name {
      max-width: 2.28rem;
      font-weight: 500;
      color: rgba(0, 0, 0, 0.9);
      font-size: 0.16rem;
      line-height: 0.22rem;
      position: absolute;
      top: 3.16rem;
      left: 0;
      z-index: 1;
      opacity: 1;
      padding-top: 0;
      width: 2.28rem;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
      word-wrap: break-word;
      white-space: break-spaces;
    }
    .book-name-box {
      position: relative;
      z-index: 2;
      opacity: 0;
      padding: 0.12rem;
      background: rgba(50, 50, 57, 0.07);
      display: block;
      border-radius: 0 0 0.08rem 0.08rem;
      transition: all 0.5s;
      .book-name-hover {
        display: block;
        font-size: 0.16rem;
        font-weight: 500;
        color: #ff375f;
        line-height: 0.22rem;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        word-wrap: break-word;
        margin-bottom: 0.1rem;
      }
      .tag-box {
        font-size: 0.12rem;
        font-weight: 400;
        gap: 0.08rem;
        display: flex;
        flex-wrap: wrap;
        height: 0.24rem;
        overflow: hidden;
        .tag-item {
          color: rgba(0, 0, 0, 0.6);
          border-radius: 0.12rem;
          padding: 0 0.1rem;
          background-color: rgba(50, 50, 57, 0.07);
          height: 0.24rem;
          line-height: 0.24rem;
          max-width: 3rem;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          transition: color 0.3s;
        }
      }
    }
  }
}
@media (max-width: 768px) {
  .container {
    .classify-container {
      background: #fff;
      display: flex;
      align-items: center;
      justify-content: space-between;
      flex-direction: row;
      height: 0.88rem;
      position: fixed;
      top: 0.88rem;
      left: 0;
      z-index: 999;
      width: 100%;
      margin: 0;
      padding-left: 0.2rem;
      .tab-container {
        flex: 1 1;
        position: relative;
        overflow: hidden;
        .tab-content {
          display: flex;
          flex-wrap: nowrap;
          flex-direction: row;
          overflow-x: auto;
          &::-webkit-scrollbar {
            display: none;
          }
          .tab-item {
            flex-shrink: 0;
            padding: 0.12rem 0.24rem;
            background: rgba(50, 50, 57, 0.07);
            border-radius: 0.08rem;
            font-size: 0.24rem;
            font-weight: 500;
            line-height: 0.33rem;
            color: rgba(0, 0, 0, 0.6);
            margin-right: 0.16rem;
            height: auto;
            min-width: auto;
          }
          .active {
            color: #ff375f;
            font-weight: 500;
            background-color: rgba(50, 50, 57, 0.07);
          }
        }
      }
      .menuTitleIcon {
        width: 0.92rem;
        height: 0.8rem;
        padding: 0.2rem 0.32rem 0.2rem 0.2rem;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
      }
      .dropdown-box {
        position: absolute;
        left: 0;
        top: 0;
        z-index: 999;
        width: 100%;
        min-height: 3rem;
        background-color: #fff;
        overflow: hidden;
        .menu-head {
          padding: 0 0.32rem;
          width: 100%;
          height: 0.88rem;
          background: #fff;
          -webkit-backdrop-filter: blur(10px);
          backdrop-filter: blur(10px);
          font-size: 0.32rem;
          font-weight: 500;
          color: rgba(0, 0, 0, 0.9);
          h4 {
            font-size: 0.32rem;
            font-weight: 500;
            color: rgba(0, 0, 0, 0.9);
          }
          img {
            width: 0.4rem;
            height: 0.4rem;
            transform: rotate(180deg);
            cursor: pointer;
          }
        }
        .menu-content {
          padding: 0.08rem 0.32rem 0.4rem;
          height: 100%;
          overflow-y: auto;
          display: grid;
          grid-template-columns: repeat(3, 1fr);
          grid-gap: 0.2rem;
          .menu-item {
            display: inline-block;
            border-radius: 0.08rem;
            padding: 0.16rem;
            text-align: center;
            font-size: 0.24rem;
            font-weight: 400;
            color: rgba(0, 0, 0, 0.6);
            height: 0.64rem;
            background: rgba(50, 50, 57, 0.07);
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            &:hover {
              color: #ff375f;
            }
          }
          .active {
            color: #ff375f;
            font-weight: 500;
            background-color: rgba(50, 50, 57, 0.07);
          }
        }
      }
    }
  }
}
</style>
